package com.fjrz.bbxwj.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.fjrz.bbxwj.app.RZApplication
import com.fjrz.bbxwj.entity.*
import com.fjrz.bbxwj.mode.api.*
import com.fjrz.bbxwj.mode.manager.UploadProgress
import com.module.lemlin.rxhttp.code
import com.module.lemlin.rxhttp.msg
import io.reactivex.rxjava3.disposables.Disposable
import org.greenrobot.eventbus.EventBus
import rxhttp.wrapper.entity.Progress
import java.io.File

class UserViewModel(application: Application) : BaseViewModel(application) {


    val uploadLiveData = MutableLiveData<HttpServiceResponse<UploadResponse>>()
    val progressLiveData = MutableLiveData<Progress>()

    /**
     * 上传视频
     * */
    fun commonUpload(path: String) {
        val progress = object : UploadProgress {
            override fun onProgress(progress: Progress) {
                progressLiveData.postValue(progress)
            }
        }
        val disposable: Disposable =
            com.fjrz.bbxwj.mode.manager.commonUpload(
                File(path),
                progress,
                ViewModelSubscribe(uploadLiveData)
            )
        dispose.add(disposable)
    }

    val userMeLiveData = MutableLiveData<HttpServiceResponse<UserMeResponse>>()
    val videoDraftLiveData = MutableLiveData<HttpServiceResponse<VideoDraftResponse>>()

    /**
     * 获取个人信息
     * */
    fun userMe() {
        val disposable: Disposable = userMe(ViewModelSubscribe(userMeLiveData))
        dispose.add(disposable)
    }

    val userInfoLiveData = MutableLiveData<HttpServiceResponse<UserInfoResponse>>()

    /**
     * 用户信息
     *
     * @param userId
     * */
    fun userInfo(userId: Int) {
        val disposable: Disposable =
            userInfo(userId.toString(), ViewModelSubscribe(userInfoLiveData))
        dispose.add(disposable)
    }

    val userSmsLiveData = MutableLiveData<HttpServiceResponse<UserSmsResponse>>()

    /**
     * 发送短信验证码
     *
     * @param mobile 手机号
     * @param event 事件：login,set_pwd
     * */
    fun userSmsSend(mobile: String, event: String) {
        val listener = object : ViewModelSubscribe<UserSmsResponse>(userSmsLiveData) {
            override fun onSuccess(resp: UserSmsResponse) {
                super.onSuccess(resp.apply { data = mobile })
            }
        }
        val disposable: Disposable = userSmsSend(mobile, event, listener)
        dispose.add(disposable)
    }


    val userSetPwdLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 设置访问密码
     *
     * @param smsCode 短信验证码
     * @param password 密码
     * @param confirmPassword 确认密码
     * */
    fun userSetPwd(
        smsCode: String, password: String, confirmPassword: String
    ) {
        val disposable: Disposable =
            userSetPwd(smsCode, password, confirmPassword, ViewModelSubscribe(userSetPwdLiveData))
        dispose.add(disposable)
    }


    var page: Int = 1
    var limit: Int = 20

    /**
     * 我的作品 / 草稿箱
     *
     * @param status 状态 0草稿箱,1待审核,2审核通过,3:审核拒绝
     * */
    fun userMyVideos(status: Int, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoDraftResponse>(videoDraftLiveData) {
            override fun onSuccess(resp: VideoDraftResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userMyVideos(status.toString(), page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }

    /**
     * 私人草稿箱视频
     *
     * @param password 访问密码
     * */
    fun userPrivateDraftVideos(password: String, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoDraftResponse>(videoDraftLiveData) {
            override fun onSuccess(resp: VideoDraftResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userPrivateDraftVideos(password, page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val passwordLiveData = MutableLiveData<String>()

    /**
     * 临时保存用户密码
     * */
    fun setPasswordSaveInterim(pwd: String?) {
        passwordLiveData.postValue(pwd)
    }


    val videoDeleteLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 删除视频
     *
     * @param videoIds 视频id,多个用逗号隔开
     * */
    fun videoDelete(videoIds: String) {
        val disposable: Disposable = videoDelete(
            videoIds, ViewModelSubscribe(videoDeleteLiveData)
        )
        dispose.add(disposable)
    }

    val userProfileLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 修改用户信息
     *
     * @param userRequest
     * */
    fun userProfile(userRequest: UserRequest) {
        val disposable: Disposable = userProfile(
            userRequest, ViewModelSubscribe(userProfileLiveData)
        )
        dispose.add(disposable)
    }

    /**
     * 关注
     *
     * @param userId
     * */
    fun userFollow(userId: Int) {
        val listener = object : ViewModelSubscribe<HttpServiceData>(MutableLiveData()) {
            override fun onSuccess(resp: HttpServiceData) {
                super.onSuccess(resp)
                userInfo(userId)
                EventBus.getDefault().post(UserIsFollow(user_id = userId, isFollow = 1))
            }

            override fun onFailure(throwable: Throwable) {
                super.onFailure(throwable)
                if (throwable.code != 401) {
                    ToastUtils.showShort(throwable.msg)
                }
            }
        }
        val disposable: Disposable = userFollow(userId.toString(), listener)
        dispose.add(disposable)
    }

    /**
     * 取消关注
     *
     * @param userId
     * */
    fun userUnFollow(userId: Int) {
        val listener = object : ViewModelSubscribe<HttpServiceData>(MutableLiveData()) {
            override fun onSuccess(resp: HttpServiceData) {
                super.onSuccess(resp)
                userInfo(userId)
                EventBus.getDefault().post(UserIsFollow(user_id = userId, isFollow = 0))
            }

            override fun onFailure(throwable: Throwable) {
                super.onFailure(throwable)
                if (throwable.code != 401) {
                    ToastUtils.showShort(throwable.msg)
                }
            }
        }
        val disposable: Disposable = userUnFollow(userId.toString(), listener)
        dispose.add(disposable)
    }


    val videoWorksLiveData = MutableLiveData<HttpServiceResponse<VideoWorksResponse>>()

    /**
     * 作品列表
     *
     * @param userId
     * */
    fun userVideos(userId: Int, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoWorksResponse>(videoWorksLiveData) {
            override fun onSuccess(resp: VideoWorksResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userVideos(userId.toString(), page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val videoLikesLiveData = MutableLiveData<HttpServiceResponse<VideoLikesResponse>>()

    /**
     * 喜欢的作品列表
     *
     * @param userId
     * */
    fun userLikeVideos(userId: Int, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoLikesResponse>(videoLikesLiveData) {
            override fun onSuccess(resp: VideoLikesResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userLikeVideos(userId.toString(), page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val userAttentionLiveData = MutableLiveData<HttpServiceResponse<UserAttentionResponse>>()

    /**
     * 关注列表
     * */
    fun userFollowList(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<UserAttentionResponse>(userAttentionLiveData) {
            override fun onSuccess(resp: UserAttentionResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userFollowList(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    /**
     * 粉丝列表
     * */
    fun userFansList(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<UserAttentionResponse>(userAttentionLiveData) {
            override fun onSuccess(resp: UserAttentionResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userFansList(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }

    /**
     * 互相关注列表
     * */
    fun userMutualFollowList(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<UserAttentionResponse>(userAttentionLiveData) {
            override fun onSuccess(resp: UserAttentionResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userMutualFollowList(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }

    /**
     * 我的关注
     * */
    fun userAttention(status: Int, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<UserAttentionResponse>(userAttentionLiveData) {
            override fun onSuccess(resp: UserAttentionResponse) {
                super.onSuccess(resp.apply {
                    loadMore = page > 1
                    this.status = status
                })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable = when (status) {
            0 -> userFollowList(page.toString(), limit.toString(), listener)
            1 -> userFansList(page.toString(), limit.toString(), listener)
            else -> userMutualFollowList(page.toString(), limit.toString(), listener)
        }
        dispose.add(disposable)
    }


    val userCertificateLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 实名认证
     *
     * @param name 姓名
     * @param identityNumber 身份证号
     * @param idCardFrontImage 身份证正面照
     * @param idCardBackImage 身份证背面照
     * */
    fun userCertificate(
        name: String, identityNumber: String, idCardFrontImage: String, idCardBackImage: String
    ) {
        val disposable: Disposable =
            userCertificate(
                name, identityNumber, idCardFrontImage, idCardBackImage,
                ViewModelSubscribe(userCertificateLiveData)
            )
        dispose.add(disposable)
    }
}